package com.anzhen.domain.service.impl;

import com.anzhen.domain.dao.TodoRecordMapper;
import com.anzhen.domain.dao.UserMapper;
import com.anzhen.domain.entity.TodoRecord;
import com.anzhen.domain.entity.TodoRecordExample;
import com.anzhen.domain.entity.User;
import com.anzhen.domain.entity.UserExample;
import com.anzhen.domain.enumType.TodoStatus;
import com.anzhen.domain.service.TodoService;
import com.anzhen.domain.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by anzhen on 2017-6-19.
 */
@Service
public class TodoServiceImpl implements TodoService {

	@Autowired
	private TodoRecordMapper todoRecordMapper;
	@Autowired
	private UserService userService;


	@Override
	public int addTodoRecord(TodoRecord todoRecord) {
		return todoRecordMapper.insertSelective(todoRecord);
	}

	@Override
	public List<TodoRecord> getTodoList(String email, TodoStatus todoStatus) {
		User user = userService.getUserByEmail(email);
		TodoRecordExample example = new TodoRecordExample();
		TodoRecordExample.Criteria criteria = example.createCriteria().andUIdEqualTo(user.getuId());
		if (todoStatus != null) {
			criteria.andTdStatusEqualTo(todoStatus.name());
		}
		example.setOrderByClause("td_create_time desc");
		return todoRecordMapper.selectByExample(example);
	}

	@Override
	public void completeTodo(int todo) {
		TodoRecord todoRecord = new TodoRecord();
		todoRecord.setTdId(todo);
		todoRecord.setTdStatus(TodoStatus.DONE.name());
		todoRecordMapper.updateByPrimaryKeySelective(todoRecord);
	}
}
